﻿using System;
using System.Data;

using VSTalk.Engine.Core.Data;

namespace VSTalk.Engine.Core.ContactInfo
{
    public class ContactInfoDataProvider : IContactInfoDataProvider
    {
        private readonly ContactInfoDA _infoDA = new ContactInfoDA();

        private static IDatabaseManager DatabaseManager
        {
            get { return AppServices.Get<IDatabaseManager>(); }
        }

        public ContactInfoDataProvider()
        {
            DatabaseManager.RegisterDA(_infoDA);
        }

        public bool HasPhoto(string jid, string hash)
        {
            return _infoDA.HasPhoto(jid, hash);
        }

        public void RecordDetails(string jid, string hash, ContactDetails details)
        {
            _infoDA.AddRecord(jid, hash,
                    details.Fullname,
                    details.Birthday.Ticks,
                    details.Description,
                    details.Url,
                    details.PhotoType,
                    details.PhotoBin);
        }

        public ContactDetails GetLatestInfo(string jid)
        {
            var tuple = _infoDA.GetLatest(jid);
            if (tuple == null)
            {
                return null;
            }
            var result = new ContactDetails();
            result.Fullname = tuple.Item1;
            result.Url = tuple.Item2;
            result.Birthday = new DateTime(tuple.Item3);
            result.Description = tuple.Item4;
            result.PhotoType = tuple.Item5;
            result.PhotoBin = tuple.Item6;
            return result;
        }
    }
}